package com.dhcc.his.service.student.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.dhcc.framework.app.service.common.AbstractBaseService;
import com.dhcc.framework.common.PageModel;
import com.dhcc.his.dao.student.StudentDao;
import com.dhcc.his.dto.student.StudentDto;
import com.dhcc.his.entity.student.Student;
import com.dhcc.his.service.student.StudentService;

/**
* 
* <p>业务描述: 请在这里写上你的业务描述</p>
* 公司:东华医为科技有限公司<br/>
* <p>版权: dhcc2019</p>
* @author: {$GenAuthor}
* @date: {$GenDate}
* @version: V1.0
 */
 
@Service("studentService")
public class StudentServiceImpl extends AbstractBaseService<Student, String> implements StudentService {

	private StudentDao studentDao;

	public StudentServiceImpl(StudentDao studentDao) {
		super(studentDao);
		this.studentDao = studentDao;
	}

	@Override
	public PageModel page(StudentDto dto) {
		Specification<Student> spec = getListSpecification(dto);
		if (dto.getPageModel()==null) {
			dto.setPageModel(new PageModel());
		}
		Page<Student> page = studentDao.findAll(spec, dto.getPageModel().getPageable());
		dto.getPageModel().setPage(page);
		return dto.getPageModel();
	}
	
	protected Specification<Student> getListSpecification(final StudentDto dto){
        return new Specification<Student>() {
            @Override
            public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicate = new ArrayList<>();
                //构造查询条件
   
                Predicate[] pre = new Predicate[predicate.size()];
                return query.where(predicate.toArray(pre)).getRestriction();
            }
        };
	}

}
